
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.URIUtil;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JTextArea;

/**
 * HTTP工具箱
 *
 * @author leizhimin 2009-6-19 16:36:18
 * @author dlf 2014 12 20 00:19  
 */
public final class HttpTookit {

//    private static final Log log = LogFactory.getLog(HttpTookit.class);
    private final javax.swing.JTextArea jTextArea1;
    private static HttpClient client;
    private static HttpMethod method;

    public HttpTookit(JTextArea jTextArea1) {
        this.jTextArea1 = jTextArea1;
        client = new HttpClient();
        method = new GetMethod();
    }

    /**
     * 执行一个HTTP GET请求，返回请求响应的HTML
     *
     * @param url 请求的URL地址
     * @param queryString 请求的查询参数,可以为null
     * @param charset 字符集
     * @param pretty 是否美化
     * @return 返回请求响应的HTML
     */
    public String doGet(String url, String queryString, String charset, boolean pretty) {
        StringBuilder response = new StringBuilder();
        client = new HttpClient();
        method = new GetMethod(url);
        try {
            if (queryString != null) //对get请求参数做了http请求默认编码，好像没有任何问题，汉字编码后，就成为%式样的字符串
            {
                method.setQueryString(URIUtil.encodeQuery(queryString));
            }
            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (pretty) {
                        response.append(line).append(System.getProperty("line.separator"));
                    } else {
                        response.append(line);
                    }
                }
                reader.close();
            }
        } catch (URIException e) {
            jTextArea1.append("执行HTTP Get请求时，编码查询字符串“" + queryString + "”发生异常！");
        } catch (IOException e) {
            jTextArea1.append("执行HTTP Get请求" + url + "时，发生异常！");
        } finally {
            method.releaseConnection();
        }
        return response.toString();
    }

    /**
     * 执行一个HTTP POST请求，返回请求响应的HTML
     *
     * @param url 请求的URL地址
     * @param params 请求的查询参数,可以为null
     * @param charset 字符集
     * @param pretty 是否美化
     * @return 返回请求响应的HTML
     */
    public String doPost(String url, Map<String, String> params, String charset, boolean pretty) {
        StringBuilder response = new StringBuilder();
        client = new HttpClient();
        method = new PostMethod(url);
        //设置Http Post数据
        if (params != null) {
            HttpMethodParams p = new HttpMethodParams();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                p.setParameter(entry.getKey(), entry.getValue());
            }
            method.setParams(p);
        }
        try {
            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (pretty) {
                        response.append(line).append(System.getProperty("line.separator"));
                    } else {
                        response.append(line);
                    }
                }
                reader.close();
            }
        } catch (IOException e) {
            jTextArea1.append("执行HTTP Post请求" + url + "时，发生异常！");
        } finally {
            method.releaseConnection();
        }
        return response.toString();
    }

    /**
     * *
     * 将json转换为map
     *
     * @param jsonString
     * @return
     * @throws JSONException
     */
    public static Map<String, String> toMap(String jsonString) throws JSONException {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonString);
        } catch (JSONException e) {
            throw new JSONException("解析失败");
        }

        Map<String, String> result = new HashMap<>();
        Iterator<?> iterator = jsonObject.keys();
        String key;
        String value;

        while (iterator.hasNext()) {

            key = (String) iterator.next();
            if (key.equals("images")) {
                continue;
            }
            value = (String) jsonObject.getString(key);
            result.put(key, value);

        }
        return result;

    }

//    public static void main(String[] args) {
//        String isbn = "9787302466833";
//        String uri = "https://api.douban.com/v2/book/isbn/:" + isbn;
//        parse(uri);
//    }
    public Map<String, String> search(String ISBN) throws JSONException {
        Map<String, String> map;
        String uri = "https://api.douban.com/v2/book/isbn/:" + ISBN;
        map = parse(uri);
        out(map);
        return map;
    }

    public Map<String, String> parse(String uri) throws JSONException {
        String y = doGet(uri, null, "UTF-8", true);
        jTextArea1.append("请求：" + uri + "\r\n返回：" + y + "\r\n");
        y = y.replace("\"", "'");  //将得到的json数据中的双引号改为单引号

        y = y.replaceAll("\\\\", "/");   //以下步骤将\改为/(url有用)
        y = y.replaceAll("//", "/");
        jTextArea1.append("处理返回：" + y + "\r\n");
//        System.out.println(y);
        Map<String, String> map = toMap(y);
//        try {

//        } catch (JSONException e) {
        // TODO Auto-generated catch block
//            System.out.println("解析错误");
//            e.printStackTrace();
//        }
        jTextArea1.setCaretPosition(jTextArea1.getText().length());

//                iteator(map);
//                print(map); 
        return map;
    }

    /**
     * *
     * 打印书的所有信息
     *
     * @param map
     */
    public static void iteator(Map<String, String> map) {
        Iterator<?> iter = map.entrySet().iterator();
        String key, value;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            key = (String) entry.getKey();
            value = (String) entry.getValue();
            System.out.println(key + "   " + value);
        }
    }

    /**
     * *
     * 打印书的关键信息
     *
     * @param map
     */
    public static void print(Map<String, String> map) {
        System.out.println("*********");
        System.out.println("书名  " + map.get("title"));
        String author = map.get("author");
        author = author.replace("[", "");
        author = author.replace("]", "");
        author = author.replace("\"", "");
        System.out.println("作者  " + author);
        System.out.println("原价格  " + map.get("price"));
        System.out.println("出版社  " + map.get("publisher"));
        System.out.println("出版日期  " + map.get("pubdate"));
        System.out.println("isbn  " + map.get("isbn10"));
        Map<String, String> map2 = null;
        try {
            map2 = toMap(map.get("images").replace("\"", "'"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("small images " + map2.get("small"));
        System.out.println("medium images " + map2.get("medium"));
        System.out.println("small large " + map2.get("large"));
    }

    /**
     * *
     * 输出书的关键信息
     *
     * @param map
     */
    public void out(Map<String, String> map) {

//        System.out.println("*********");
        jTextArea1.append("书名  " + map.get("title") + "\r\n");
        String author = map.get("author");
        author = author.replace("[", "");
        author = author.replace("]", "");
        author = author.replace("\"", "");
        jTextArea1.append("作者      " + author + "\r\n");
        jTextArea1.append("原价格    " + map.get("price") + "\r\n");
        jTextArea1.append("出版社    " + map.get("publisher") + "\r\n");
        jTextArea1.append("出版日期  " + map.get("pubdate") + "\r\n");
        jTextArea1.append("isbn10   " + map.get("isbn10") + "\r\n");
        try {
            Map<String, String> map2;
            map2 = toMap(map.get("图片 ").replace("\"", "'"));
            jTextArea1.append("小图 " + map2.get("small") + "\r\n");
            jTextArea1.append("中图 " + map2.get("medium") + "\r\n");
            jTextArea1.append("大图 " + map2.get("large") + "\r\n");

        } catch (JSONException | NullPointerException e) {
        }
    }

}
